<template>
  <div id="app">
    <router-view v-if="isRouterAlive" />
  </div>
</template>

<script>
import { mapActions } from "vuex";
export default {
  provide() {
    return {
      reload: this.reload
    };
  },
  data() {
    return {
      isRouterAlive: true
    };
  },
  mounted() {
    window.addEventListener("unload", this.saveState); //监听unload方法，如果重载页面就把state存入sessionStorage
  },
  created() {
    if (sessionStorage.state) {
      //判断有无临时缓存
      this.$store.replaceState(
        Object.assign(this.$store.state, JSON.parse(sessionStorage.state))
      );
      delete sessionStorage["state"]; //删除临时缓存
    }
  },
  methods: {
    //刷新页面
    reload() {
      this.isRouterAlive = false;
      this.$nextTick(function() {
        this.isRouterAlive = true;
      });
    },
    saveState() {
      sessionStorage.setItem("state", JSON.stringify(this.$store.state)); //把state存入sessionStorage
    },
    ...mapActions([
      "gettoken", // 将 `this.gettoken(amount)` 映射为 `this.$store.dispatch('gettoken', amount)`
      "getuserUid",
      "getcircleUrl"
    ])
  }
};
</script>

<style lang="less">
body {
  margin: 0;
  padding: 0;
  font-size: 14px;
  font-family: "微软雅黑";
}
</style>


